package com.withoutwords.ui;


import com.withoutwords.R;
import com.withoutwords.engine.BitmapCache;
import com.withoutwords.engine.Common;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.Round;
import com.withoutwords.ui.drawing.DrawingControllerFactory;

import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;

public class ActorPlayingActivity extends BasePlayinyActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		showAllWords = true;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actor_playing);

		initViews();
	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		super.initViews();
		ImageButton btn = (ImageButton) findViewById(R.id.btnSendActing);
		btn.setOnClickListener(this);

		btn = (ImageButton) findViewById(R.id.btnDrawingSettings);
		btn.setOnClickListener(this);

		btn = (ImageButton) findViewById(R.id.btnUndo);
		btn.setOnClickListener(this);

		btn = (ImageButton) findViewById(R.id.btnShowGuesses);
		btn.setOnClickListener(this);

		btn = (ImageButton) findViewById(R.id.btnErase);
		btn.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.btnSendActing: {

		/*	Bitmap bmp = mCanvasView.getBitmap(true);
		
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			bmp.compress(Bitmap.CompressFormat.PNG, 80, stream);
			byte[] byteArray = stream.toByteArray();
*/
			try
			{
			
			byte[] byteArray = mDrawingController.getDrawingBytesToSend();
			GameController.getInstance().sendActingData(selectedWordIndex,
					byteArray);
			mDrawingController.clearCurrentDrawing();
			}
			catch (Exception e) {
				// TODO: handle exception
			}
			// Log.e("sent Drawing Bytes", new String(byteArray));

			break;
		}
		case R.id.btnShowGuesses: {
			showGuesses();
			break;
		}
		case R.id.btnDrawingSettings: {
			try {
				mDrawingController.onSettingsRequested();
			} catch (Exception e) {
				// TODO: handle exception
			}

			break;
		}
		case R.id.btnUndo: {
			try {
			mDrawingController.undo();
			} catch (Exception e) {
				// TODO: handle exception
			}
			// mCanvasView.
			break;
		}
		/*
		 * case R.id.btnRedo: { mCanvasView.redo(); break; }
		 */

		case R.id.btnErase: {
			try {

				mDrawingController.onEraseModeActiviated();
			} catch (Exception e) {
				// TODO: handle exception
			}
			break;
			// case text view
		}
		}

	}

	@Override
	protected void setBitmaps() {
		// TODO Auto-generated method stub
		super.setBitmaps();

		setLayoutBitmap(R.id.lyActor, R.drawable.drawing);

	}

	@Override
	protected void unsetBitmaps() {
		// TODO Auto-generated method stub
		super.unsetBitmaps();
		unsetLayoutBitmaps(R.id.lyActor);

	}

	@Override
	protected void releaseBitmaps() {
		// TODO Auto-generated method stub
		super.releaseBitmaps();
		/*
		 * BitmapCache .getCacheInstance().deleteDrawable(R.drawable.intro);
		 */
		BitmapCache.getCacheInstance().deleteDrawable(R.drawable.drawing);
	}

	
@Override
public void onNewDataRecieved(Intent data) {
	// TODO Auto-generated method stub
	super.onNewDataRecieved(data);
	Round r = (Round)data.getSerializableExtra(Common.roundCode);
	if(r != null)
	{
		round  = r;
	}
	else
	{
		round  = GameController.getInstance().getCurrentGame().getCurrentRound();
	}
	
}
	@Override
	protected void initDrawingViews() {
		// TODO Auto-generated method stub
		//super.initDrawingViews();
		if(mDrawingController == null)
		{
			mDrawingController = DrawingControllerFactory.createDrawingView();
		}
		FrameLayout fly = (FrameLayout) findViewById(R.id.canvas_container_id);
		mDrawingController.initializeViews(this, fly, false);
		ViewGroup tools = (ViewGroup)findViewById(R.id.lyTools);
		if(tools != null)
		{
			tools.bringToFront();
		}
		// mCanvasView.setOnHistoryChangeListener(this);
		// mSettingView.setOnSettingChangedListener(this);
	}

	public void onWordPressed(int wordIndex) {
		selectedWordIndex = wordIndex;

	}

	@Override
	protected byte[] getDrawingBitmapData() {
		// TODO Auto-generated method stub
		return mDrawingController.getDrawingBytesToShare();
	}
	
}
